package commandPattern;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @author： pang-yun
 * @date： 2021-12-31 13:26
 * 功能目的 ：用于  invoker
 */

public class RemoteController {

    //记录所有的 操作
    static ConcurrentHashMap<Integer, ICommand> commandOn = new ConcurrentHashMap<>();

    private RemoteController() {
    }

    private static RemoteController instance = new RemoteController();

    public static RemoteController getInstance() {
        return instance;
    }

    //用于记录上一步操作进行撤回
    private int last;

    // 给 我们的按钮设置需要的命令
    public void setCommandOn(int index, ICommand command) {
        commandOn.putIfAbsent(index, command);
    }

    // 按下开关
    public void onButton(int index) {
        ICommand execute = commandOn.getOrDefault(index, new NoCommand());
        last = index;
        execute.execute();
    }

    // 撤回上一步操作
    public void unDo() {
        commandOn.getOrDefault(last, new NoCommand()).undo();
    }

}
